3D Graphics Programming with QuickDraw 3D 1.5.4
Previous | QD3D Book | Overview | Chapter Contents | Next |
Listing 5 illustrates how to create, use, and dispose of pick objects. It defines a function, MyHandleClickInWindow , that takes a window pointer and an event record and handles mouse clicks in that window.
TQ3Status MyHandleClickInWindow (CGrafPtr myWindow, EventRec myEvent)
{
TQ3WindowPointPickData myWPPickData;
TQ3PickObject myPickObject;
unsigned long myNumHits;
unsigned long myIndex;
Point myPoint;
TQ3Point2D my2DPoint;
TQ3ViewObject myView;
/*Get the window coordinates of a mouse click.*/
SetPort(myWindow);
myPoint = myEvent.where; /*get location of mouse click*/
GlobalToLocal(&myPoint); /*convert to window coordinates*/
my2DPoint.x = myPoint.h; /*configure a 2D point*/
my2DPoint.y = myPoint.v;
/*Set up picking data structures.*/
/*Set sorting type: objects nearer to pick origin are returned first.*/
myWPPickData.data.sort = kQ3PickSortNearToFar;
myWPPickData.data.mask = kQ3PickDetailMaskPickID | kQ3PickDetailMaskXYZ |
kQ3PickDetailMaskObject;
myWPPickData.data.numHitsToReturn = kQ3ReturnAllHits;
myWPPickData.point = my2DPoint;
myWPPickData.vertexTolerance = 2.0;
myWPPickData.edgeTolerance = 2.0;
/*Create a new window-point pick object.*/
myPickObject = Q3WindowPointPick_New(&myWPPickData);
myView = MyGetViewFromWindow(myWindow); /*increments reference count*/
/*Pick a group object.*/
Q3View_StartPicking(myView, myPickObject);
do {
Q3DisplayGroup_Submit(gGroup, myView);
} while (Q3View_EndPicking(myView) == kQ3ViewStatusRetraverse);
/*See whether any hits occurred.*/
if (Q3Pick_GetNumHits(myPickObject, &myNumHits) == kQ3Failure || myNumHits==0) {
Q3Object_Dispose(myPickObject);
return;
}
/* Process each hit */
for (myIndex = 0; myIndex = myNumHits; myIndex++) {
TQ3Point3D xyzPoint;
unsigned long pickID;
TQ3Object object;
/* Get validMask first */
if (Q3Pick_GetPickDetailValidMask(myPickObject, myIndex, &validMask)
== kQ3Failure) {
break;
}
if (! ((validMask & kQ3PickDetailMaskXYZ) &&
(validMask & kQ3PickDetailMaskPickID) &&
(validMask & kQ3PickDetailMaskObject))) {
continue;
}
/* Get world space intersection, pick ID, and geometry object reference */
object = NULL;
status = Q3Pick_GetPickDetailData (myPickObject, myIndex,
kQ3PickDetailMaskXYZ, &xyzPoint);
status = Q3Pick_GetPickDetailData (myPickObject, myIndex,
kQ3PickDetailMaskPickID, &pickID);
status = Q3Pick_GetPickDetailData (myPickObject, myIndex,
kQ3PickDetailMaskObject, &object);
/* Operate on xyzPoint, pickID, and object */
...
if (object != NULL) {
Q3Object_Dispose(object);
}
}
/*Dispose of all hits in the hit list.*/
Q3Pick_EmptyHitList(myPickObject);
/*Dispose of the pick object.*/
Q3Object_Dispose(myPickObject);
/*Dispose of the view object.*/
Q3Object_Dispose(myView);
}
Note that the call to Q3Pick_EmptyHitList is redundant, because disposing of a pick object (by calling Q3Object_Dispose ) also disposes of its associated hit list. The call is included in Listing 5 simply to illustrate how to call Q3Pick_EmptyHitList . You would, however, need to call to Q3Pick_EmptyHitList if you wanted to reuse the associated pick object in another pick operation.
Previous | QD3D Book | Overview | Chapter Contents | Next |